package com.kfm.base.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {
    public static void main(String[] args) {

        try {
            // server
            DatagramSocket server = new DatagramSocket(8888);

            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);

            while (true){
                server.receive(packet); // 接收内容

                System.out.println("接收到信息来源：" + packet.getAddress());
                System.out.println("信息来源端口：" + packet.getPort());
                System.out.println("接收到信息长度：" + packet.getLength());
                System.out.println("接收到信息内容：" + new String(packet.getData(), 0, packet.getLength()));
            }


        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
